//
//  RegisterViewController.m
//  XDBaseFrameWork
//
//  Created by xindao on 14-9-28.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterVerifyCodeViewController.h"
#import "UserDelegateViewController.h"

@interface RegisterViewController ()
{
    UIButton *finishButton;
}
@end

@implementation RegisterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.leftBtn.hidden =NO;
    self.titleLabel.text = @"注册";
     self.contentView.backgroundColor = UIColorFromRGB(0xffffefa);
    
//    finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    finishButton.frame =  CGRectMake(10, 5, 40, 30);
//    [finishButton setTitle:@"完成" forState:UIControlStateNormal];
//    [finishButton addTarget:self action:@selector(finishButtonPressed) forControlEvents:UIControlEventTouchUpInside];
//    UIView *assoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 40)];
//    assoryView.backgroundColor = [UIColor redColor];
//    [assoryView addSubview:finishButton];
    
    registerField = [[UITextField alloc]initWithFrame:CGRectMake(20, 30,(UI_SCREEN_WIDTH - 20*2), 40)];
    registerField.placeholder = @"点击输入要注册的手机号";
    registerField.textAlignment = NSTextAlignmentLeft;
    registerField.tag = 333;
    registerField.clearButtonMode = UITextFieldViewModeWhileEditing;
    registerField.keyboardType = UIKeyboardTypeNumberPad;
    registerField.delegate = self;
//    registerField.inputAccessoryView  = assoryView;
    registerField.borderStyle = UITextBorderStyleRoundedRect;
//    UIImageView *pwdLeftView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
//    pwdLeftView.image = [UIImage imageNamed:@"direction_wheel"];
//    pwdLeftView.backgroundColor = [UIColor orangeColor];
//    registerField.leftView = pwdLeftView;
//    registerField.leftView.hidden = NO;
//    registerField.hidden = NO;
    registerField.leftViewMode  = UITextFieldViewModeAlways;
    [self.contentView addSubview:registerField];
    
    comfirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    comfirmButton.frame = CGRectMake(15, height_y(registerField)+30, (UI_SCREEN_WIDTH - 2*15), 40);
    [comfirmButton setBackgroundImage:LOADIMAGE(@"pressedBg@2x") forState:UIControlStateNormal];
    [comfirmButton setTitle:@"注册" forState:UIControlStateNormal];
    [comfirmButton addTarget:self action:@selector(comfirmButtonPressed) forControlEvents:UIControlEventTouchUpInside];
   [comfirmButton setBackgroundImage:[UIImage imageNamed:@"pressedBg"] forState:UIControlStateHighlighted];
    comfirmButton.titleLabel.font = [UIFont systemFontOfSize:17.0f];
    [self.contentView addSubview:comfirmButton];
    
    UILabel *userDelegateInfo = [[UILabel alloc]init];
    userDelegateInfo.frame =  CGRectMake(15, height_y(comfirmButton), 240, 30);
    userDelegateInfo.textAlignment = NSTextAlignmentLeft;
    userDelegateInfo.textColor = UIColorFromRGB(0x999999);
    userDelegateInfo.font = [UIFont systemFontOfSize:14.0f];
    userDelegateInfo.text = @"点击注册表示你同意《用户使用协议》";
    userDelegateInfo.attributedText = [XDTools getAcolorfulStringWithTextArray:@[@"用户使用协议"] Color:RGBA(188, 136,55, 1) Font:[UIFont systemFontOfSize:14] AllText:userDelegateInfo.text];
    [self.contentView addSubview:userDelegateInfo];
    
    UIButton * userinfobtn = [UIButton initButton:CGRectMake(145, height_y(comfirmButton)+3, 100, 30) btnImage:nil btnTitle:nil];
    [userinfobtn addTarget:self action:@selector(gotoUserDelegateVC) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:userinfobtn];
    
}

#pragma mark
#pragma mark 去用户注册协议页面
-(void)gotoUserDelegateVC
{
    UserDelegateViewController * uvc = [[UserDelegateViewController alloc] init];
    [self.navigationController pushViewController:uvc animated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return  YES;
}
-(void)comfirmButtonPressed
{
    comfirmButton.enabled = NO;
    if (![XDTools chickIphoneNumberRight:registerField.text]) {
        comfirmButton.enabled = YES;
        [XDTools showTips:@"请输入正确的手机号码" toView:self.view];
    }else{
         [self sendMessageToThePhone];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)finishButtonPressed
{
    [registerField resignFirstResponder];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
    if (![XDTools chickIphoneNumberRight:textField.text]) {
        [XDTools showTips:@"请输入正确的手机号码" toView:self.view];
    }
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length >=11) {
        [XDTools showTips:@"请输入11位手机号" toView:self.view];
        registerField.text = [textField.text substringToIndex:10];
        return  YES;
    }
    
    return  YES;
}

#pragma mark＝＝＝请求数据 ======
-(void)sendMessageToThePhone
{
    
//    UIAlertView *alertShow = [[UIAlertView alloc]initWithTitle:@"确认手机号码" message:[NSString stringWithFormat:@"是否发送验证码到该手机\n%@"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好", nil];
//    [alertShow show];
    
    if(![XDTools NetworkReachable]){
        comfirmButton.enabled = YES;
        [XDTools showTips:brokenNetwork toView:self.contentView];
        return;
    }
    NSDictionary *requestDic = @{@"MobilePhone":registerField.text,@"Type":@"R"};
    ASIHTTPRequest *request = [XDTools postRequestWithDict:requestDic API:API_SMSGET];
    __weak ASIHTTPRequest * mrequest = request;
    [request setCompletionBlock:^{
        comfirmButton.enabled = YES;

        NSDictionary *resultDic = [XDTools JSonFromString:[mrequest responseString]];
        DDLOG(@"res....%@",resultDic);
        if ([[resultDic objectForKey:@"status"] intValue]==1) {
            RegisterVerifyCodeViewController *verifyVC = [[RegisterVerifyCodeViewController alloc]init];
            verifyVC.verifyCode = [[resultDic objectForKey:@"data"]objectForKey:@"Code"];
            verifyVC.phoneNumber = registerField.text;
            
            [XDTools showTips:@"验证码已发送，请注意查收" toView:[UIApplication sharedApplication].keyWindow];
            [self.navigationController pushViewController:verifyVC animated:YES];
        }else{
            [XDTools showTips:[resultDic objectForKey:@"msg"] toView:self.view];
        }
        
    }];
    [request setFailedBlock:^{
        comfirmButton.enabled = YES;

        [XDTools hideProgress:self.contentView];
        if (mrequest.error.code == 2) {
            [XDTools showTips:@"网络请求超时" toView:self.view];
        }
    }];
    [request setTimeOutSeconds:12];
    [request startAsynchronous];
}

//-(void)sendMessageToThePhone
//{
//  
//    //http://123.57.16.119/index.php?m=Api&c=Customer&a=getCustomerOrder&CustomerID=''
//    //http://123.57.16.119/index.php?m=Api&c=Index&a=sendsms
//    
//    if(![XDTools NetworkReachable]){
//        [XDTools showTips:brokenNetwork toView:self.contentView];
//        return;
//    }
//    NSDictionary *requestDic = @{@"MobilePhone":@"13301382017",};
//    ASIHTTPRequest *request = [XDTools postRequestWithDict:requestDic API:API_SMSGET];
////    ASIHTTPRequest *request = [XDTools getRequestWithDict:requestDic API:API_SMSGET];
//    __weak ASIHTTPRequest * mrequest = request;
//    [request setCompletionBlock:^{
//        NSDictionary *resultDic = [XDTools JSonFromString:[mrequest responseString]];
//        DDLOG(@"res....%@",resultDic);
//        if ([[resultDic objectForKey:@"status"] intValue]==1) {
//            //                [XDTools showTips:[requestDic objectForKey:@"msg"] toView:self.contentView];
//   
//            
//        }else{
//            [XDTools showErrorTips:[resultDic objectForKey:@"msg"] toView:self.view];
//        }
//        
//    }];
//    [request setFailedBlock:^{
//        [XDTools hideProgress:self.contentView];
//        if (mrequest.error.code == 2) {
//            [XDTools showTips:@"网络请求超时" toView:self.view];
//        }
//    }];
//    [request setTimeOutSeconds:12];
//    [request startAsynchronous];
//    
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
